package com.whoimi.admin.repository;

import com.whoimi.admin.entity.UserNotice;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * @author whoimi
 * @since 2025-07-29
 **/
@Repository
public interface UserNoticeRepository extends PagingAndSortingRepository<UserNotice, Long> {

/*    @Query("""
             SELECT
                 un.id,
                 un.user_id,
                 un.notice_id,
                 un.is_read,
                 un.create_time,
                 n.title,
                 n.content
             FROM wiic_admin.sys_user_notice un
             JOIN wiic_admin.sys_notice n ON un.notice_id = n.id
             WHERE un.user_id = :userId
            """)
    Page<UserNotice> findUserNoticesWithDetails(@Param("userId") Long userId, Pageable pageable);*/

    Page<UserNotice> findByUsername(String username, Pageable pageable);

    @Modifying
    @Query("""
            UPDATE wiic_admin.sys_user_notice un
            SET un.is_read = 1
            WHERE un.username = :username
            and un.is_read=0
            """)
    Long markAsRead(@Param("username") String username);

}
